package com.poposchool.project.service.impl;

import com.poposchool.project.common.Constants;
import com.poposchool.project.mapper.FileMapper;
import com.poposchool.project.exception.ServiceException;
import com.poposchool.project.entity.Files;
import com.poposchool.project.service.FilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Wang Jie
 * @Date 2022/8/3 14:48
 * @Version 1.0
 */
@Service
@Slf4j
public class FilesServiceImpl implements FilesService {
    @Resource
    FileMapper fileMapper;

    @Override
    public void insert(Files files) {
        try {
            fileMapper.insert(files);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public Files getFileMd5(String md5) {
        try {
            Files file = fileMapper.getFileMd5(md5);
            return file;
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public List<Files> getPageFiles(Integer pageNum, Integer pageSize, String name) {
        try {
            List<Files> list = fileMapper.getPageFiles((pageNum - 1) * pageSize,pageSize,name);
            return list;
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void batchDeleteFiles(List<Integer> ids) {
        try {
            fileMapper.batchDeleteFiles(ids);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public Integer getAllCount() {
        try {
            return fileMapper.getAllCount();
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void deleteFile(Integer id) {
        try {
            fileMapper.deleteFile(id);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void updateEnable(Integer id, Boolean enable) {
        try {
            fileMapper.updateEnable(id,enable);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public List<String> getEnableFile() {
        try {
            return fileMapper.getEnableFile();
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }
}
